package com.zlzxm.shoesmarket.view.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.util.ZStringHelp;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.activity.manage.bundle.BundleGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSize;
import com.zlzxm.shoesmarket.view.adapter.entity.EntitySizeInfo;
import com.zlzxm.shoesmarket.view.widget.GoodsInfoView;
import com.zlzxm.shoesmarket.viewmodel.VMRepositoryGoodsDetail;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityGoodsDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/15/21
 * <br>
 * 详情
 */
public class ACRepositoryGoodsDetail extends BaseActivity implements View.OnClickListener, OnItemClickListener {



    public static void start(Activity activity, ImageView sharedElement,
                             String id, String face, String name, String code, String brand,
                             String tags){

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo(id,face,name,code,brand,tags);

        start( activity ,sharedElement,bundleGoodsInfo);

    }


    public static void start(Activity activity, ImageView sharedElement,
                             BundleGoodsInfo bundleGoodsInfo){

        Intent intent = new Intent(activity,ACRepositoryGoodsDetail.class);

        intent.putExtra(StartBundleKey.KEY_GOODS_INFO,bundleGoodsInfo);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if(sharedElement != null) {

                activity.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, sharedElement,
                        activity.getString(R.string.transition_goods_pic)).toBundle());
            }else {

                activity.startActivity(intent);

            }

        }else {

            activity.startActivity(intent);

        }

    }

    private VMRepositoryGoodsDetail vmRepositoryGoodsDetail;

    private ADGoodsSize adGoodsSize = new ADGoodsSize();

    private TextView tvInOutNote,tvInOut,tvMove;

    private RecyclerView recyclerView;


    private TextView tvNum,tvCost,tvProfit,tvRealProfit,tvProfitRate,tvTransfer,tvDynamic;

    private GoodsInfoView goodsInfoView;

    private ImageView ivEdit;

    @Override
    protected void initView() {

        super.initView();

        setContentView(R.layout.ac_repository_goods_detail);

        recyclerView  = ZViewHelp.findById(this,R.id.rvSize);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        goodsInfoView  = ZViewHelp.findById(this,R.id.goodsInfoView);

        tvNum  = ZViewHelp.findById(this,R.id.tvNum);

        tvCost  = ZViewHelp.findById(this,R.id.tvCost);

        tvProfit  = ZViewHelp.findById(this,R.id.tvProfit);

        tvRealProfit  = ZViewHelp.findById(this,R.id.tvRealProfit);

        tvProfitRate  = ZViewHelp.findById(this,R.id.tvProfitRate);


        ZViewHelp.setOnClickListener(this,R.id.ivLeft,this);

        tvTransfer  = ZViewHelp.setOnClickListener(this,R.id.tvTransfer,this);

        tvInOutNote  = ZViewHelp.setOnClickListener(this,R.id.tvInOutNote,this);
        tvInOut  = ZViewHelp.setOnClickListener(this,R.id.tvInOut,this);
        tvMove  = ZViewHelp.setOnClickListener(this,R.id.tvMove,this);
        tvDynamic  = ZViewHelp.setOnClickListener(this,R.id.tvDynamic,this);

        ivEdit  = ZViewHelp.setOnClickListener(this,R.id.ivEdit,this);


        adGoodsSize.setOnItemClickListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();

        vmRepositoryGoodsDetail = new ViewModelProvider(this)
                .get(VMRepositoryGoodsDetail.class);

        vmRepositoryGoodsDetail.liveGoodsInfo.observe(this, this::onChanged);

    }

    @Override
    protected void initData() {
        super.initData();
        BundleGoodsInfo info = (BundleGoodsInfo) getIntent().getSerializableExtra(StartBundleKey.KEY_GOODS_INFO);

        EntityGoodsDetail entityGoodsDetail = new EntityGoodsDetail();
        entityGoodsDetail.setName(info.getName());
        entityGoodsDetail.setCode(info.getCode());
        entityGoodsDetail.setBrand(info.getBrand());
        entityGoodsDetail.setFace(info.getFace());
        entityGoodsDetail.setSizeInfos(info.getTags());

        vmRepositoryGoodsDetail.liveGoodsInfo.setValue(entityGoodsDetail);
        recyclerView.setAdapter(adGoodsSize);
        vmRepositoryGoodsDetail.requestSize(info.getCode());
    }


    private void onChanged(EntityGoodsDetail entity) {

        goodsInfoView.updateFace(entity.getFace());
        goodsInfoView.updateName(entity.getName());
        goodsInfoView.updateCode(entity.getCode());
        goodsInfoView.updateBrand(entity.getBrand());
        tvNum.setText(entity.getNum());
        tvCost.setText(entity.getCost());
        tvProfit.setText(entity.getProfit());
        tvRealProfit.setText(entity.getRealProfit());
        tvProfitRate.setText(entity.getProfitRate());
        List<String> strings =  ZStringHelp.splitToList(entity.getSizeInfos(), Constant.Split.size_split);

        List<EntitySizeInfo> sizeInfos = new ArrayList<>();

        for (String string : strings) {
            EntitySizeInfo entitySizeInfo = new EntitySizeInfo();
            entitySizeInfo.setSize(string);

            sizeInfos.add(entitySizeInfo);
        }

        adGoodsSize.setList(sizeInfos);

    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvTransfer){

            startTransferGoods();

        }else if(v.getId() == R.id.tvInOutNote){


            startACGoodsInOutNote();

        }else if(v.getId() == R.id.ivEdit){

            startACSaveEdit();

        }else if(v.getId() == R.id.tvInOut){

            startACBatchInOut();

        }else if(v.getId() == R.id.tvMove){

            startACSizeMove();

        }else if(v.getId() == R.id.tvDynamic){

            startACMarketDynamic();

        }else if(v.getId() == R.id.ivLeft){

            finish();
        }

    }


    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        startACGoodsSizeDetail();
    }


    private void startTransferGoods(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());

        ActivityIndex.startACTransferGoods(this, bundleGoodsInfo);


    }

    private void startACGoodsInOutNote(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACGoodsInOutNote(this,bundleGoodsInfo);


    }


    private void startACGoodsSizeDetail(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACGoodsSizeDetail(this,bundleGoodsInfo);


    }


    private void startACSaveEdit(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACSaveEdit(this,bundleGoodsInfo);


    }


    private void startACBatchInOut(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACBatchInOut(this,bundleGoodsInfo);


    }

    private void startACSizeMove(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACSizeMove(this,bundleGoodsInfo);


    }


    private void startACMarketDynamic(){

        EntityGoodsDetail entity = vmRepositoryGoodsDetail.liveGoodsInfo.getValue();

        BundleGoodsInfo bundleGoodsInfo = new BundleGoodsInfo("1",entity.getFace(),
                entity.getName(),entity.getCode(),entity.getBrand(),entity.getSizeInfos());


        ActivityIndex.startACMarketDynamic(this,bundleGoodsInfo);


    }

}
